from typing import Optional, Any, Dict
from pydantic import BaseModel, field_validator, ConfigDict, model_validator


class CommonText_request(BaseModel):
    title: str
    sketch: Optional[str] = None
    text: str
    author: int
    images: Optional[list] = None
    index_image: Optional[str] = None
    resource: str
    tips: Optional[list] = None
    visibility: Optional[str] = "all"
    model_config = ConfigDict(extra='allow')

    file_fields: Dict[str, Any] = {}

    @model_validator(mode='after')
    def capture_custom_fields(cls, values):

        prefix = "file"
        file_fields = set(values.model_fields.keys())

        input_fields = set(values.__dict__.keys())
        file_fields = {
            field: values.__dict__[field]
            for field in input_fields
            if field.startswith(prefix) and field not in file_fields
        }

        values.file_fields = file_fields
        for field in file_fields:
            delattr(values, field)

        return values